import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_menu/app_config.dart';
import 'package:flutter_menu/data/obx_store.dart';
import 'package:flutter_menu/menu_page.dart';
import 'package:flutter_menu/res/app_design.dart';
import 'package:flutter_menu/res/langs/app_i10n.dart';
import 'package:flutter_menu/route/app_route.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get_storage/get_storage.dart';
import 'package:get/get.dart';
import 'menu_binding.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  catchUnHandleEx();
  await GetStorage.init();
  Get.put(await initStore());
  await AppConfig.instance.init();

  if (!kDebugMode) {
    debugPrint = (String? message, {int? wrapWidth}) {};
  }
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]).then((_) {
    runApp(const MyApp());
  });
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
        designSize: const Size(800, 1340),
        builder: (_, __) => GetMaterialApp(
              title: AppConfig.instance.config.title,
              theme: AppStyle.light,
              translations: I10N(),
              supportedLocales: supportedLocales,
              localizationsDelegates: const [
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
              ],
              navigatorObservers: [FlutterSmartDialog.observer,AppRoutesObserver.observer],
              locale: PlatformDispatcher.instance.locale,
              initialRoute: MenuPages.Menu_Order,
              getPages: MenuPages.routes,
              initialBinding: MenuBinding(),
              builder: FlutterSmartDialog.init(),
            ));
  }
}

void catchUnHandleEx(
    {FlutterExceptionHandler? onError, ErrorCallback? callback}) {
  FlutterError.onError = onError ??
      (FlutterErrorDetails details) {
    print(details);
//获取 widget build 过程中出现的异常错误
// logger.e("Widget", details.exception, details.stack);
      };
  PlatformDispatcher.instance.onError = callback ??
      (error, stack) {
        print(stack);
// logger.e("Global", error, stack);
        return true;
      };
}
